package com.net.framework.help.utils;

import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;

import java.io.File;

/**
 * @author zhangxiaobin
 * @email zhangxiaobin@yipintech.com
 * @date 创建时间：2015年7月26日 下午10:03:00
 * @version 1.1.0
 * @explain 说明
 */
public class FileUitl {

	/***
	 * 将图片uri地址转化成File
	 * 
	 * @param activity
	 * @param uri
	 * @return
	 */
	public static File getUriToFile(Activity activity, Uri uri) {
		// String[] proj = { MediaStore.Images.Media.DATA };
		// Cursor actualimagecursor = activity.managedQuery(uri, proj, null,
		// null, null);
		// int actual_image_column_index =
		// actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
		// actualimagecursor.moveToFirst();
		// String img_path =
		// actualimagecursor.getString(actual_image_column_index);

		File file = new File(getRealPathFromURI(activity, uri));
		return file;
	}

	private static String getRealPathFromURI(Activity activity, Uri contentUri) {
		String res = null;
		String[] proj = { MediaStore.Images.Media.DATA };
		Cursor cursor = activity.getContentResolver().query(contentUri, proj, null, null, null);
		if (cursor.moveToFirst()) {
			;
			int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
			res = cursor.getString(column_index);
		}
		cursor.close();
		return res;
	}

}
